import { defineStore } from "pinia";
import { computed, reactive, ref, toRefs } from "vue";
import apis from '@/apis/index';


export const useStaffStore = defineStore("staff", () => {

  /**
   * 响应式数据
   */
  const isCollapsedRef = ref(false);
  const staffListRef = ref([]);
  const staffTotalRef = ref(0);
  const staffDetailRef = ref<any>(null);
  const isLoadingRef = ref(true);
  const isClosedRef = ref(false);   // 左侧搜索栏是否关闭

  /**
   * 方法
   */
  //@ 修改搜索栏关闭状态
  function setIsClosed(bool: boolean) {
    isClosedRef.value = bool;
  }

  //@ 修改折叠状态
  function updateIsCollapsed(params: boolean) {
    isCollapsedRef.value = params;
  }

  //@ 更新员工列表
  function updateStaffList(params: []) {
    staffListRef.value = params;
    // console.log(staffListRef,'reffffffffff')
  } 

  //@ 更新员工总数
  function updateStaffTotal(params: number) {
    staffTotalRef.value = params;
  }

  //@ 更新员工详情
  function updateStaffDetail(params: any) {
    staffDetailRef.value = params;
  }

  //@ 更新加载的状态
  function updateIsLoading(params: boolean, delay: number = 1500) {
    setTimeout(() => {
      isLoadingRef.value = params;
    }, delay);
  }

  //@ 获取员工列表
  async function getStaffList(page = 1, size = 10, queryData = {}) {
    try {
      const params = {
        page,
        size,
        queryData,
      }
      const data = await apis.staff.getStaffList_API(params);
      console.log(data,'getStaffList_API');
      if (data) {
        // console.log(data.staffList,'dddddddddddddd')
        updateStaffList(data.staffList);
        updateStaffTotal(data.staffTotal);
        updateIsLoading(false);
        // console.log(staffListRef,'ref')
      }
    } catch (error) {
      console.log(error,'error-获取员工列表失败');
    }
  }

  async function getStaffDetail(staffId = 0) {
    try {
      const res = await apis.staff.getStaffDetail_API(staffId);
      console.log(res,'detail')
      if (res) {
        updateStaffDetail(res);
      }
      return res;
    } catch (error) {
      console.log(error, 'error-getStaffDetail')
    }
  }

  /**
   * 返回暴露值
   */
  return {
    isClosedRef,
    setIsClosed,
    isCollapsedRef,
    isLoadingRef,
    staffListRef,
    staffTotalRef,
    staffDetailRef,
    getStaffList,
    updateIsCollapsed,
    getStaffDetail
  }
});